﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
using Cashier_Barcode;

namespace Cashier.prints
{
    public class PrintBarcode
    {
        /// <summary>
        /// 打印商品条码
        /// </summary>
        /// <param name="print"></param>
        /// <param name="e"></param>
        /// <returns>当前打印行的索引</returns>
        public static int PrintGoodsBarcode128(PrintParameter print, PrintPageEventArgs e, int printCopies)
        {
            try
            {
                int rowCount = print.Dgv.Rows.Count;
                if (print.IsPrintSelectedRows)
                    rowCount = print.Dgv.SelectedRows.Count;

                int x = (int)e.PageSettings.HardMarginX + 1;
                int y = (int)e.PageSettings.HardMarginY + 1;
                int printAreaWidth = e.PageBounds.Width - x * 2; //打印区域宽度
                int printAreaHeight = e.PageBounds.Height - y * 2; //打印区域宽度
                int markWidth = printAreaWidth / print.BarcodeMarksInRow;
                int markHeight = print.BarcodeMarkHeight;
                int marksInRow = print.BarcodeMarksInRow;
                int barcodeCopies = print.BarcodeCopies;                

                using (e.Graphics)
                {
                    //打印内容
                    if (print.StartRowIndex < rowCount)
                    {
                        String cellContent = String.Empty;
                        Pen rectPen = new Pen(Brushes.White, 1);

                        int printRows = 0; //打印行数
                        int printColumns = 0; //打印列数
                        String barcodeTitle = String.Empty; //标签名称
                        String barcodeContent = String.Empty; //标签内容

                        //定义矩形
                        Rectangle rect = new Rectangle(x, y, markWidth, markHeight);

                        for (int i = print.StartRowIndex; i < rowCount; i++)
                        {
                            DataGridViewRow row = null;
                            if (print.IsPrintSelectedRows)
                                row = print.Dgv.SelectedRows[i];
                            else
                                row = print.Dgv.Rows[i];                           
                            //int printCopies = 0; //每个标签打印份数
                            barcodeTitle = row.Cells["name"].Value.ToString();
                            if (row.Cells["norm"].Value != null && !String.IsNullOrEmpty(row.Cells["norm"].Value.ToString()))
                                barcodeTitle += "/" + row.Cells["norm"].Value.ToString();
                            barcodeContent = row.Cells["barcode"].Value.ToString();
                            
                            while (true)
                            {                               
                                if (printCopies == barcodeCopies)
                                {
                                    printCopies = 0;
                                    break;
                                }
                                if (printColumns > 0 && printColumns == marksInRow)
                                {
                                    printRows++;
                                    printColumns = 0;

                                    if ((printRows * markHeight + y + markHeight) > printAreaHeight)
                                    {
                                        e.HasMorePages = true;
                                        break;
                                    }
                                }
                                
                                rect.X = printColumns * markWidth + x;
                                rect.Y = printRows * markHeight + y;
                                e.Graphics.DrawRectangle(rectPen, rect);
                                BarcodeFactory.DrawBarcode128(e.Graphics, rect.X, rect.Y+2, true, barcodeTitle, barcodeContent, 0, 40);

                                printCopies++;
                                printColumns++;
                            }

                            if ((printRows * markHeight + y + markHeight) > printAreaHeight)
                            {
                                if (i < (rowCount - 1))
                                {
                                    print.StartRowIndex = i;
                                    e.HasMorePages = true;
                                    break;
                                }
                                else
                                    e.HasMorePages = false;
                            }
                        }
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return printCopies;
        }
    }
}
